var DAO=require('./DAO')
var orderDAO={
    getOrderList:function(userId,callback){
        DAO('select orderinfos.*,productName,productPic,price,orderDate from (orderinfos join product on orderinfos.productId=product.productId) join `order` on orderinfos.orderId=`order`.orderId where  `order`.userId=?',[userId],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    getOrderDetail:function(orderIds,callback){
        DAO('select orderinfos.*,productName,productPic,price,orderDate from (orderinfos join product on orderinfos.productId=product.productId) join `order` on orderinfos.orderId=`order`.orderId where  `order`.orderId in(?)',[orderIds],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    delOrderDetail:function(orderId,callback){
        DAO('delete from orderinfos where orderId=?',[orderId],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
   },
   delOrder:function(orderId,callback){
       DAO('delete from `order` where orderId=?',[orderId],function(err,results){
        if(err){
            callback(err,null)
        }else{
            callback(null,results)
        }
       })
   },
//    订单表
   addOrder:function(userId,callback){
        DAO('insert into `order`(userId,orderDate) value(?,now())',[userId],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
   },
   addOrderDetail:function(ids,callback){
      DAO('insert into orderinfos(productId,orderId,count,address) select productId,orderId,count,address from (shop join `order` on shop.userId=`order`.userId)  join userinfos on shop.userId=userinfos.userId and shopId in (?) and orderId=?',[ids.shopIds,ids.orderId],function(err,results){
        if(err){
            callback(err,null)
        }else{
            callback(null,results)
        }
      })
   }

}

module.exports=orderDAO